﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace _5
{
    public partial class Form1 : Form
    {
        private Crawler _crawler;
        private readonly BackgroundWorker _backgroundWorker=new BackgroundWorker();
        public Form1()
        {
            InitializeComponent();
            Initialize();
            _backgroundWorker.WorkerReportsProgress = true;
        }

        private void Initialize()
        {
            listView1.Items.Clear();
            listView1.HotTracking = true;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_crawler==null)
            {
                _crawler = new Crawler(textBox1.Text);
            }
            _backgroundWorker.DoWork+=_crawler.Crawl;
            _backgroundWorker.ProgressChanged += UpdateTelephone;
            _backgroundWorker.RunWorkerAsync();
        }

        private void UpdateTelephone(object sender, ProgressChangedEventArgs e)
        {
            var pair = ((string, string))e.UserState;
            var item = new ListViewItem(pair.Item1);
            item.SubItems.Add(pair.Item2);
            listView1.Items.Add(item);
        }
    }
}